﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Conqueror.API;

namespace Conqueror.WP.Model
{
    public static class UserLogin
    {
        public static event EventHandler LoginSuccess;
        public static event EventHandler LoginFailure;

        #region Login Welcomes

        private static string[] validatingAccount = new string[] {
            "logging you in",
            "checking you in",
            "searching troop roster",
            "arming the cannons",
            "arming the troops",
            "surveying the battleground",
            "preparing for the assault",
            "recruiting soldiers",
            "conscripting citizens",
            "welcome back commander",
            "evaluating battle plan",
            "fueling the tanks",
            "launching fighters",
            "prepare for war",
            "summoning the legions",
            "today is a good day to die",
            "the return of the warlord",
            "in the presence of my enemies",
            "do you want to die without a scar",
            "never give in",
            "marching off to war",
            "this day we fight"
        };

        private static Random random = new Random();

        public static string GetValidatingAccountString()
        {
            return validatingAccount[random.Next(validatingAccount.Length)].ToUpper();
        }

        #endregion

        private static async Task DownloadUsersGames(string username)
        {

            List<Game> games = await Conqueror.API.Game.GetUserGames(username);
        }

        public static void AttemptUserLogin(ViewModel.Login.LoginViewModel lvm)
        {
            Task.Run(async () =>
            {
                if (!ValidateUser(lvm.Username))
                {
                    if (LoginFailure != null)
                        LoginFailure(null, new EventArgs());
                    return;
                }

                Player validUser = await Player.GetPlayerAsync(lvm.Username);
                if (validUser != null)
                {
                    //lvm.LoginMessage = UserLogin.GetDownloadingGameString();
                    try
                    {
                        await UserLogin.DownloadUsersGames(validUser.Username);
                        await Map.GetMapAsync();
                    }
                    catch
                    {
                        if (LoginFailure != null)
                            LoginFailure(null, new EventArgs());
                        return;
                    }

                    if (LoginSuccess != null)
                    {
                        LoginSuccess(validUser, new EventArgs());
                    }
                }
                else
                {
                    if (LoginFailure != null)
                        LoginFailure(null, new EventArgs());
                }
            });
        }

        private static bool ValidateUser(string username)
        {
            if (string.IsNullOrWhiteSpace(username))
                return false;
            if (username.Contains(',')) //Commas are not valid
                return false;

            return true;
        }
    }
}
